using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace LS.GameKit
{
    public class SwitcherCommandSender : CommandSender
    {
        public bool oneShot = false;
        public CommandType commandType;
        public CommandType commandNegativeType;

        private bool m_ReceiverTrigged = false;
        private bool m_EventModuleTrigged = false;
        private bool m_UEventTrigged = false;

        [SerializeField]
        private bool m_IsOn = false;
        public bool IsOn
        {
            get { return m_IsOn; }
            set
            {
                this.SetValue(value);
            }
        }

        protected override bool CanSend()
        {
            if (!this.IsActive()) return false;
            return !oneShot || (!m_ReceiverTrigged && !m_EventModuleTrigged && !m_UEventTrigged);
        }

        protected override bool SendCommand(CommandReceiver receiver, params object[] values)
        {
            if (receiver == null) return false;
            m_ReceiverTrigged = true;
            receiver.ReceiveByObject(GetCurrentCommand(), values);
            return true;
        }

        protected override bool SendCommandByEventModule(string key, params object[] values)
        {
            if (string.IsNullOrEmpty(key)) return false;
            FireEvent(key, GetCurrentCommand(), values);
            m_EventModuleTrigged = true;
            return true;
        }

        protected override bool SendCommandByUnityEvent(UnityEvent<CommandType, object[]> unityEvent, params object[] values)
        {
            if (unityEvent == null) return false;
            unityEvent.Invoke(GetCurrentCommand(), values);
            m_UEventTrigged = true;
            return true;

        }

        public CommandType GetCurrentCommand()
        {
            if (m_IsOn) return commandType;
            else return commandNegativeType;
        }

        public void SetValue(bool isOn)
        {
            if (m_IsOn == isOn) return;
            m_IsOn = isOn;
            this.Send();
        }

        public void SetValueWithoutNotify(bool isOn)
        {
            if (m_IsOn == isOn) return;
            m_IsOn = isOn;
        }

        public void Switch()
        {
            if (!IsActive())
                return;

            IsOn = !IsOn;
        }
    }
}
